//
// Created by 吴竹亚 on 2023/4/11.
//

#include "types.h"

void initLink(NODE **head) {
    *head = (NODE *) malloc(sizeof(NODE));
    (*head)->next = NULL;
    (*head)->data = NULL;
    (*head)->prior = NULL;
}

void addNode(NODE *head, char c) {
    NODE *p = head;
    while (p->next) {
        p = p->next;
    }
    MyData *data = NULL;
    NODE *temp = NULL;
    data = (MyData *) malloc(sizeof(MyData));
    temp = (NODE *) malloc(sizeof(NODE));
    data->c = c;
    temp->data = data;
    temp->prior = p;
    p->next = temp;
    temp->next = NULL;
}

void show(NODE *head) {
    NODE *p = head->next;
    while (p) {
        printf("%c", p->data->c);
        p = p->next;
    }
    printf("\n");
}

void repToLink(NODE *head, char *rep) {
    for (int i = 0; i < strlen(rep); ++i) {
        addNode(head, rep[i]);
    }
}

int length(NODE *head) {
    int sum = 0;
    NODE *p = head;
    while (p->next) {
        sum++;
        p = p->next;
    }
    return sum;
}

void addPriorNode(NODE *head, char c) {
    NODE *p = (NODE *) malloc(sizeof(NODE));
    MyData *data = (MyData *) malloc(sizeof(MyData));
    data->c = c;
    p->data = data;
    p->next = head->next;
    p->prior = head;
    if (head->next) {
        head->next->prior = p;
    }
    head->next = p;
}

NODE *lastNode(NODE *head) {
    NODE *p = head;
    while (p->next) {
        p = p->next;
    }
    return p;
}

void getStr(NODE node, char *str) {
    if (node.next) {
        *str = node.next->data->c;
        getStr(*node.next, str + 1);
    }
}
